package com.google.code.timetrail.backend;


import java.util.List;
import java.util.Random;

public class PlayerDeathEvent extends Event{
    /**
	 * 
	 */
	private static final long serialVersionUID = 5020L;
	private Person randomDeadPerson;
    private Control gameControl;
    List<Person> players;
    int removedPersonIndex;
    
    public PlayerDeathEvent(Control gameControl){
        this.gameControl = gameControl;
        Random rand = new Random();
        players = gameControl.getMembers();
        if(players.size() != 1){
        removedPersonIndex = rand.nextInt(players.size() - 1) + 1;
        randomDeadPerson = players.get(removedPersonIndex);
            if(!randomDeadPerson.isDead() && randomDeadPerson.isSick() && randomDeadPerson.getDeath() < rand.nextDouble()){
                super.formatMessage(randomDeadPerson + " has died in the time stream.");
            } else {
                super.messageText = getDummyText();
            }
        } else {
            //only one player left
            super.messageText = getDummyText();
        }
    }
    
    public void Action(){
        players.remove(removedPersonIndex);
        randomDeadPerson.die();
        players.add(randomDeadPerson);
        gameControl.setMembers(players);
    }
}
